package com.lanlian.cayman.utils;

import android.util.Log;
import android.widget.Toast;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.cayman.proto.ProtoManager;
import com.lanlian.cayman.socket.MySocketManager;
import com.lanlian.cayman.socket.SocketCallImpl;
import com.oms.rpc.protobuf.KLineHqReq;
import com.oms.rpc.protobuf.KLineHqResp;
import com.oms.rpc.protobuf.OMSTradeReq;
import com.oms.rpc.protobuf.OMSTradeRsp;

public class KlneUtil {
     BaseActivity context;
    public interface IcbCall
    {
        public void comple(Object obj);
    };
    public  void getKline(BaseActivity ba,String code,String time,IcbCall icb)
    {
        context=ba;
        ProtoManager pm=new ProtoManager();
        KLineHqReq.Builder builder=pm.getKLineHqReq();
        builder.setStrcontractno(code);
        if(time!=null) {
            builder.setStrklinetype(time);
        }
        builder.setLimit(200);
        //下面的方法必须最后调用
        OMSTradeReq.Builder oms=pm.getRequst(3);
        OMSTradeReq om=oms.build();




        SocketCallImpl sc=new SocketCallImpl(context){
            @Override
            public void message(byte[] data) {

                try {
//                    String s1="";
//                    for(int i=0;i<17;i++)
//                    {
//                        s1=s1+" "+data[i];
//                    }
//                    Log.e("start",s1);
//                    String s2="";
//                    for(int i=data.length-17;i<data.length;i++)
//                    {
//                        s2=s2+" "+data[i];
//                    }
//                    Log.e("end",s2);
                    OMSTradeRsp oms=  OMSTradeRsp.parseFrom(data);



                    if(oms.getRequestStatus()==0)
                    {
                        KLineHqResp resp=oms.getKlinehqResp();

                        icb.comple(resp);
                    }
                    else
                    {
                        Toast.makeText(context,oms.getRequestStatus()+":"+oms.getStatusInfo(),Toast.LENGTH_LONG).show();
                        Log.e(getClass().getName(),oms.getRequestStatus()+":"+oms.getStatusInfo());
                    }

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    Toast.makeText(context,e.getMessage(),Toast.LENGTH_LONG).show();

                }
                finally {

                }

            }
        };
        MySocketManager ms=new MySocketManager();
        ms.setCongLian(true);
        ms.sendData(om.toByteArray(),sc);
    }
    public  void getFenshi(BaseActivity ba,String code,String  time,IcbCall icb)
    {
        context=ba;
        ProtoManager pm=new ProtoManager();
        KLineHqReq.Builder builder=pm.getKLineHqReq();
        builder.setStrcontractno(code);
        if(time!=null) {
            builder.setStrklinetype("1");
        }
       // builder.setLimit(1);
        //下面的方法必须最后调用
        OMSTradeReq.Builder oms=pm.getRequst(5);
        OMSTradeReq om=oms.build();




        SocketCallImpl sc=new SocketCallImpl(context){
            @Override
            public void message(byte[] data) {

                try {
                    OMSTradeRsp oms=  OMSTradeRsp.parseFrom(data);



                    if(oms.getRequestStatus()==0)
                    {
                        KLineHqResp resp=oms.getKlinehqResp();

                        icb.comple(resp);
                    }
                    else
                    {
                        Toast.makeText(context,oms.getRequestStatus()+":"+oms.getStatusInfo(),Toast.LENGTH_LONG).show();
                        Log.e(getClass().getName(),oms.getRequestStatus()+":"+oms.getStatusInfo());
                    }

                }
                catch (Exception e)
                {
                    e.printStackTrace();

                }
                finally {

                }

            }
        };
        MySocketManager ms=new MySocketManager();
        ms.setCongLian(true);
        ms.sendData(om.toByteArray(),sc);
    }
}
